// 解析 Content-Disposition 获取文件名
export function parseFilenameFromCD(contentDisposition, fallback = 'download.bin') {
    if (!contentDisposition) return fallback
    const mStar = /filename\*\s*=\s*UTF-8''([^;]+)/i.exec(contentDisposition)
    if (mStar && mStar[1]) {
      try { return decodeURIComponent(mStar[1]) } catch (_) {}
    }
    const mPlain = /filename\s*=\s*"?([^"]+)"?/i.exec(contentDisposition)
    if (mPlain && mPlain[1]) return mPlain[1]
    return fallback
  }
  
  // 触发浏览器保存
  export function saveBlob(blob, filename = 'download.bin') {
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = filename
    a.style.display = 'none'
    document.body.appendChild(a)
    a.click()
    a.remove()
    URL.revokeObjectURL(url)
  }
  